package model.bo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.fileupload.FileItem;

import common.GlobalConstant;
import model.bean.Account;
import model.dao.ManageAccountDAO;

public class AccountSettingBO {

	
	ManageAccountDAO manageAccountDAO = new ManageAccountDAO();
	
	public int updatePassword(Account account, String password) {
		return manageAccountDAO.changePassword(account,password);
	}


	public Account getAccount(String nameAccount) {
		return manageAccountDAO.getAccount(nameAccount);
	}


	public int updateInfoAccount(Account acc, String fileAvatar, InputStream is) {
		
		try {
			if(fileAvatar.length() > 1 && is != null){
				// Save to folder image
				File uploadDir = new File(GlobalConstant.UPLOAD_DIRECTORY);
	            if (!uploadDir.exists()) {
	                uploadDir.mkdir();
	            }
	            String filePath = uploadDir + fileAvatar;
				OutputStream os = new FileOutputStream(filePath);
				byte[] buffer = new byte[1024];
	            int bytesRead;
	            //read from is to buffer
	            while((bytesRead = is.read(buffer)) !=-1){
	                os.write(buffer, 0, bytesRead);
	            }
	            is.close();
	            os.flush();
	            os.close();
	            
	            File fi = new File(filePath);
	            System.out.println(fi.getAbsolutePath());
			}
	            
		} catch (Exception e) {
				e.printStackTrace();
		}
		
		acc.setAvatar(fileAvatar);
		return manageAccountDAO.updateInfoAccount(acc);
	}
	
	

}
